package cn.wp.pet.service.impl;

import cn.wp.basic.service.impl.BaseServiceImpl;
import cn.wp.pet.domain.Pet;
import cn.wp.pet.domain.PetDetail;
import cn.wp.pet.domain.PetType;
import cn.wp.pet.mapper.PetDetailMapper;
import cn.wp.pet.mapper.PetMapper;
import cn.wp.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        // 设置状态 默认不上架
        pet.setState(0);
        // 先保存 返回新增数据的主键
        super.add(pet);
        System.out.println(pet.getType().getId());

        // 获取PetDetail对象
        PetDetail petDetail = pet.getPetDetail();
        // 设置Pet_id的值
        petDetail.setPet_id(pet.getId());
        // 再保存
        petDetailMapper.save(petDetail);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 先删除t_pet_detail表中的数据
        petDetailMapper.delByPetId(id);
        // 再删除t_pet表数据
        super.delete(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet_id(pet.getId());
        petDetailMapper.update(petDetail);
    }

    /**
     * 批量上架
     * @param ids
     */
    @Override
    @Transactional
    public void onSale(List<Long> ids) {
        petMapper.onSale(ids);
    }

    /**
     * 批量下架
     * @param ids
     */
    @Override
    @Transactional
    public void offSale(List<Long> ids) {
        petMapper.offSale(ids);
    }
}
